﻿//页面加载
$(document).ready(function () {
    $("#btnReset").bind("click", btnReset_click);
    $("#btnSearch").bind("click", BindDataList);
    $("#btnCreate").bind("click", btnCreate_click);
    $("#btnUserGroup").bind("click", btnUserGroup_click);
    $("#btnDeleteAll").bind("click", btnDeleteAll_click);
    $("#checkall").bind("click", CheckAll_click);
    BindDataList();
})
//绑定数据
function BindDataList() {
    var groupID = $.trim($("#GroupName").val());
    var parameters = MakeParameters("WeChatUserController", "GetPageList", decodeURIComponent(groupID));
    var page = new Pagger(parameters, BindDataToTable);
    PageIndexChanging(0);
}
function BindDataToTable(result) {
    if (Pager.RecountCount == -1) {
        $("#errorDataTD").show();
        return;
    }
    else {
        $("#errorDataTD").hide();
    }
    var iCount = 0;
    var $table = $("#TableList");
    var $template = $("#template");
    $table.find(".contentRow").remove();
    $.each(result, function (i, n) {
        iCount++;
        var row = $template.clone();
        row.css("display", "");
        //数据项
        row.find("#td_checkbox").html("<input type='checkbox' name='checkbox'  class='chk' value='" + n.AID + "'/>");
        row.find("#td_HeadPicture").html("<img src='" + n.HeadPicture + "' style='Width:50px;Height:50px;vertical-align:middle;margin:5px;border:1px solid' />");
        row.find("#td_NickName").html("<a href='/WeChatUser/Show?action=Details&ID=" + n.AID + "'>" + n.NickName + "</a>");
        if (n.Sex == 1) {
            row.find("#td_Sex").html("男");
        }
        else {
            row.find("#td_Sex").html("女");
        }
        if (n.VipCardCode == null || n.VipCardCode == "") {
            row.find("#td_VipCardCode").html("<span class='cf60'>未绑卡</span>");
            row.find("#btnBind").val("绑 卡");
            row.find("#btnBind").bind("click", { ID: n.OpenId }, btnBind_click);
        }
        else {
            row.find("#td_VipCardCode").html(n.VipCardCode);
            row.find("#btnBind").val("解 绑");
            row.find("#btnBind").bind("click", { ID: n.OpenId, NickName: n.NickName }, btnUnBind_click);
        }
        row.find("#td_UserGroupName").html(n.UserGroupName);
        //操作
        //row.find("#btnSetMenu").bind("click", { ID: n.AID }, btnSetMenu_click);
        row.find("#btnEdit").bind("click", { ID: n.AID }, btnEdit_click);

        row.attr("class", "contentRow t_c");
        row.appendTo($table);
    });
    if (iCount == 0) {
        $("#emptyDataTD").show();
    }
    else {
        $("#emptyDataTD").hide();
    }
}
//全选
function CheckAll_click() {
    var chk = $(this);
    chk.parent().parent().parent().next().children().find("input:checkbox").attr("checked", chk.prop("checked"));
}
//重置界面
function btnReset_click() {
    $("#txtTitle").val("");
}


//创建: 从微信获取订阅号所有用户
function btnCreate_click() {
    // window.location.href = "/WeChatUser/Create";
    if (confirm("即将从微信获取所有用户，你确定本次操作吗？")) {
        $.post("/WeChatUser/Create", function (result) {
            if (result == "OK") {
                PageIndexChanging(Pager.CurrentPage);
            }
            else {
                alert("同步失败，请稍后重试!");
            }
        });
    }
}
//同步分组数据
function btnUserGroup_click() {
    if (confirm("即将从微信获取所有用户，你确定本次操作吗？")) {
        $.post("/WeChatUser/SyncUserGroup", function (result) {
            if (result.code == "OK") {
                //更新select 控件
                $("#GroupName").empty();
                $("#GroupName2").empty();
                var v_option = "";
                $.each(result.data, function (i, item) {
                    v_option = v_option + "<option value='" + item.AID + "'>" + item.Dic_Value + "</option>";
                })
                $("#GroupName").html(v_option);
                $("#GroupName2").html(v_option);
            }
            else {
                alert("同步失败，请稍后重试!");
            }
        });
    }
}

//绑卡
function btnBind_click(e) {
    var Id = e.data.ID;
    window.location.href = "/WeChatUser/BindCard?openId=" + Id;
}
//解绑
function btnUnBind_click(e) {
    var v_openID = e.data.ID;
    var v_nickName = e.data.NickName;
    if (confirm("你确认要解除用户[ " + v_nickName + " ]?绑定的会员卡吗？")) {
        $.post("/WeChatUser/UnBindCard", { openId: v_openID }, function (result) {
            if (result.code == "OK") {
                alert(result.msg);
                PageIndexChanging(Pager.CurrentPage);
            }
            else {
                alert(result.msg);
            }
        }, "json");
    }
}

//编辑
function btnEdit_click(e) {
    var Id = e.data.ID;
    window.location.href = "/WeChatUser/Show?Action=Update&ID=" + Id;
}
//批量删除
function btnDeleteAll_click() {
    var strIDs = "";
    $(this).parent().parent().parent().next().children().find(".chk").each(function (i, item) {
        if ($(item).prop("checked")) {
            //字符串ID
            //strIDs += "\""+$(item).val() + "\";"; 
            //整形ID
            strIDs += $(item).val() + ",";
        }
    });
    if (strIDs == "") {
        alert("请至少选择一条数据!");
        return;
    }
    if (confirm("你确认要删除该数据?")) {
        $.post("/WeChatUser/Delete", { IDs: strIDs }, function (result) {
            if (result == "OK") {
                alert("删除成功!");
                PageIndexChanging(Pager.CurrentPage);
            }
            else {
                alert("删除失败，请稍后重试!");
            }
        });
    }
}
//删除单项
function btnDelete_click(e) {
    var Id = e.data.ID;
    if (confirm("确认要删除该数据吗?")) {
        $.post("/WeChatUser/Delete", { IDs: Id }, function (result) {
            if (result == "OK") {
                alert("删除成功!");
                PageIndexChanging(Pager.CurrentPage);
            }
            else {
                alert("删除失败，请稍后重试!");
            }
        });
    }
}
//时间字符串截取
function CutString(str, index, length) {
    if (str != null && str != undefined) {
        if (str.length >= length) {
            if (length > 10) {
                var str1 = str.substring(index, length);
                return str1.replace("T", " ");
            }
            else {
                return str.substring(index, length);
            }
        }
        else {
            return str;
        }
    }
    else {
        return "";
    }
}
